#!/usr/bin/python

import sys
import getopt

usage = '''run-lrex.py {-g|-p|-v} spec [stap|out]
    Where:
        -g Generate SystemTap script
        -p Parse output and print requests
        -v Parse output and vizualize requests
'''

try:
    opts, args = getopt.getopt(sys.argv[1:], 'gpv')
except getopt.GetoptError:
    print >> sys.stderr, usage
    sys.exit(1)
opts = [opt[0] for opt in opts]


from lrex.spec import Spec, LrexSpecError
from lrex.trace import Trace
from lrex.viz import RQVizualizer

try:
    s = Spec()
    s.parse(args[0])
except LrexSpecError as mse:
    print >> sys.stderr, str(mse)
    mse.dump()
    
    sys.exit(1)

if '-g' in opts:
    if len(args) > 1:
        outf = file(args[1])
    else:
        outf = sys.stdout
    
    s.generate(outf)
else:
    t = Trace(s, file(args[1]))
    t.parse()
    
    if '-v' in opts:
        viz = lambda rq: rq.events and rq.sub_rq
        
        v = RQVizualizer(t)
        v.vizualize(viz)
        v.vizualize_all(viz)
    elif '-p' in opts:
        t.pprint()